#include <iostream>
#include <unistd.h>
#include <string>
#include <string.h>

using namespace std;

int main()
{
    int fd[2];
    pid_t pid;
    char message[] = "I am father";
    if(pipe(fd) < 0) {
        perror("pipe error");
        return 1;
    }

    pid = fork();
    if(pid < 0) {// 错误
        perror("fork error");
        return 1;
    } else if(pid > 0) { // 父进程
        close(fd[0]);
        write(fd[1], message, strlen(message) + 1);
    } else { // 子进程
        close(fd[1]);
        char read_buffer[1024];
        read(fd[0], read_buffer, sizeof read_buffer);
        cout << "Message from father: " << read_buffer << endl;
        close(fd[0]);
    }

    return 0;
}